package com.jingjiu.homeservicesbiz.service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingjiu.homeservicesapi.Vo.HouseServiceOrderVo;
import com.jingjiu.homeservicesapi.Vo.UserVo;
import com.jingjiu.homeservicesapi.domain.HouseService;
import com.jingjiu.homeservicesapi.domain.HouseServiceOrder;
import com.jingjiu.homeservicesapi.domain.UserInfo;
import com.jingjiu.homeservicesbiz.mapper.HouseServiceOrderMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 家政订单：记录家政服务的订单详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-16
 */
@Service
public class HouseServiceOrderServiceImpl extends ServiceImpl<HouseServiceOrderMapper, HouseServiceOrder>
{
    @Autowired
    private HouseServiceOrderMapper houseServiceOrderMapper;

    @Autowired
    private UserInfoServiceImpl userInfoService;

    public Page<HouseServiceOrderVo> selectPage(Page<HouseServiceOrder> page, HouseServiceOrder houseServiceOrder) {
        LambdaQueryWrapper<HouseServiceOrder> queryWrapper = new LambdaQueryWrapper<>();

        // 模糊查询服务人员姓名
        if (StringUtils.isNotBlank(houseServiceOrder.getServicePersonName())) {
            queryWrapper.like(HouseServiceOrder::getServicePersonName, houseServiceOrder.getServicePersonName());
        }

        // 查询手机号
        if (StringUtils.isNotBlank(houseServiceOrder.getPhone())) {
            queryWrapper.eq(HouseServiceOrder::getPhone, houseServiceOrder.getPhone());
        }

        // 查询状态
        if (StringUtils.isNotBlank(houseServiceOrder.getStatus())) {
            queryWrapper.eq(HouseServiceOrder::getStatus, houseServiceOrder.getStatus());
        }

        // 可根据服务时间做区间查询（如果你提供了开始时间和结束时间字段）
        // queryWrapper.between(...);

        // 排序
        queryWrapper.orderByDesc(HouseServiceOrder::getId);

        // 查询数据
        Page<HouseServiceOrder> houseServiceOrderPage = this.page(page, queryWrapper);

        // 获取所有UserIds
        List<Long> userIds = houseServiceOrderPage.getRecords().stream()
                .map(HouseServiceOrder::getUserId)
                .distinct()
                .collect(Collectors.toList());

        // 直接遍历查询每个订单的用户信息
        List<HouseServiceOrderVo> houseServiceOrderVoList = houseServiceOrderPage.getRecords().stream()
                .map(order -> {
                    HouseServiceOrderVo orderVo = new HouseServiceOrderVo();
                    BeanUtils.copyProperties(order, orderVo);

                    // 直接调用 userInfoService 获取用户信息
                    UserVo userInfo = userInfoService.selectUserInfoById(order.getUserId());
                    if (userInfo != null) {
                        orderVo.setOrderName(userInfo.getName());
                        orderVo.setOrderPhone(userInfo.getPhone());// 设置用户信息
                    }
                    return orderVo;
                })
                .collect(Collectors.toList());

        // 将转换后的Vo设置回Page对象
        Page<HouseServiceOrderVo> resultPage = new Page<>();
        resultPage.setRecords(houseServiceOrderVoList);
        resultPage.setTotal(houseServiceOrderPage.getTotal());


        return resultPage;
    }


    /**
     * 查询家政订单：记录家政服务的订单详情
     * 
     * @param id 家政订单：记录家政服务的订单详情主键
     * @return 家政订单：记录家政服务的订单详情
     */

    public HouseServiceOrderVo selectHouseServiceOrderById(Long id)
    {
        HouseServiceOrder houseServiceOrder = houseServiceOrderMapper.selectHouseServiceOrderById(id);
        UserVo userVo = userInfoService.selectUserInfoById(houseServiceOrder.getUserId());
        HouseServiceOrderVo houseServiceOrderVo = new HouseServiceOrderVo();
        BeanUtils.copyProperties(houseServiceOrder, houseServiceOrderVo);
        houseServiceOrderVo.setOrderName(userVo.getName());
        houseServiceOrderVo.setOrderPhone(userVo.getPhone());
        return houseServiceOrderVo;
    }

//    /**
//     * 查询家政订单：记录家政服务的订单详情列表
//     *
//     * @param houseServiceOrder 家政订单：记录家政服务的订单详情
//     * @return 家政订单：记录家政服务的订单详情
//     */
//
//    public List<HouseServiceOrder> selectHouseServiceOrderList(HouseServiceOrder houseServiceOrder)
//    {
//        return houseServiceOrderMapper.selectHouseServiceOrderList(houseServiceOrder);
//    }

    /**
     * 新增家政订单：记录家政服务的订单详情
     * 
     * @param houseServiceOrder 家政订单：记录家政服务的订单详情
     * @return 结果
     */

    public int insertHouseServiceOrder(HouseServiceOrder houseServiceOrder)
    {
        return houseServiceOrderMapper.insertHouseServiceOrder(houseServiceOrder);
    }

    /**
     * 修改家政订单：记录家政服务的订单详情
     * 
     * @param houseServiceOrder 家政订单：记录家政服务的订单详情
     * @return 结果
     */

    public int updateHouseServiceOrder(HouseServiceOrder houseServiceOrder)
    {
        return houseServiceOrderMapper.updateHouseServiceOrder(houseServiceOrder);
    }



    /**
     * 删除家政订单：记录家政服务的订单详情信息
     * 
     * @param id 家政订单：记录家政服务的订单详情主键
     * @return 结果
     */

    public int deleteHouseServiceOrderById(Long id)
    {
        return houseServiceOrderMapper.deleteHouseServiceOrderById(id);
    }




    public List<HouseServiceOrderVo> selectHouseServiceOrderByUserId(Long userId) {
            // 1. 构建查询条件
            LambdaQueryWrapper<HouseServiceOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(HouseServiceOrder::getUserId, userId)
                    .orderByDesc(HouseServiceOrder::getServiceTime); // 按创建时间倒序

            // 2. 查询所有符合条件的订单
            List<HouseServiceOrder> orders = list(queryWrapper);

            // 3. 转换为VO列表
            if (CollectionUtils.isEmpty(orders)) {
                return Collections.emptyList();
            }

            return orders.stream()
                    .map(order -> {
                        HouseServiceOrderVo vo = new HouseServiceOrderVo();
                        BeanUtils.copyProperties(order, vo);
                        // 直接调用 userInfoService 获取用户信息
                        UserVo userInfo = userInfoService.selectUserInfoById(order.getUserId());
                        if (userInfo != null) {
                            vo.setOrderName(userInfo.getName());
                            vo.setOrderPhone(userInfo.getPhone());// 设置用户信息
                        }
                        return vo;
                    })
                    .collect(Collectors.toList());
        }



    public List<HouseServiceOrderVo> selectHouseServiceOrderByUserServerId(Long userServerId) {
        // 1. 构建查询条件
        LambdaQueryWrapper<HouseServiceOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseServiceOrder::getUserServerId, userServerId)
                .orderByDesc(HouseServiceOrder::getServiceTime); // 按创建时间倒序

        // 2. 查询所有符合条件的订单
        List<HouseServiceOrder> orders = list(queryWrapper);

        // 3. 转换为VO列表
        if (CollectionUtils.isEmpty(orders)) {
            return Collections.emptyList();
        }

        return orders.stream()
                .map(order -> {
                    HouseServiceOrderVo vo = new HouseServiceOrderVo();
                    BeanUtils.copyProperties(order, vo);
                    // 直接调用 userInfoService 获取用户信息
                    UserVo userInfo = userInfoService.selectUserInfoById(order.getUserId());
                    if (userInfo != null) {
                        vo.setOrderName(userInfo.getName());
                        vo.setOrderPhone(userInfo.getPhone());// 设置用户信息
                    }
                    return vo;
                })
                .collect(Collectors.toList());
    }

}
